package cz.muni.fi.pb138.avradiation.export.xml;

import cz.muni.fi.pb138.avradiation.Sensor.Sensor;
import java.math.BigDecimal;
import java.net.URI;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author David Simansky
 */
public class XMLexportImlp implements XMLexport{

    private Document doc;

    @Override
    public void initDocument() throws ParserConfigurationException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.newDocument();
   }


    @Override
    public void processDocument(Collection<Sensor> sensorsList) throws ParserConfigurationException {
        initDocument();
        Element sensors = doc.createElement("sensors");
        doc.appendChild(sensors);
        for (Sensor s : sensorsList) {
            Element sensor = doc.createElement("sensor");
            sensors.appendChild(sensor);
            sensor.setAttribute("id", s.getName());
            Element location = doc.createElement("location");
            sensor.appendChild(location);
            location.setTextContent(s.getLocation());
            Element unit = doc.createElement("unit");
            sensor.appendChild(unit);
            unit.setTextContent(s.getUnit());
            if(s.getDescription() != null) {
                Element description = doc.createElement("description");
                sensor.appendChild(description);
                description.setTextContent(s.getDescription());
            }
            Element measures = doc.createElement("measures");
            sensor.appendChild(measures);
            Map<Long, BigDecimal> measuresMap = s.getMeasuredValues();
            for(Long key : measuresMap.keySet()) {
                Element measure = doc.createElement("measure");
                measures.appendChild(measure);
                Date date = new Date(key);
                measure.setAttribute("time", date.toString());
                if ((measuresMap.get(key) == null) || (measuresMap.get(key).toString().equals("0E-9"))){
                    measure.setTextContent("");
                } else {
                    measure.setTextContent(measuresMap.get(key).toString());
                }
                
            }
        }
    }


    @Override
    public void exportToXML(URI uri, Collection<Sensor> sensors) throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
        processDocument(sensors);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(uri.toString());
        transformer.transform(source, result);
    }
}
